define(function () {
    'use strict';

    function ctrl($scope,$filter,$ionicPopup,$q,$timeout,$ionicScrollDelegate,$stateParams,
                  CommonService,LocalCacheService,InfoService,WebService,$ionicLoading) {

        $scope.channels = {};
        $scope.vm = {};
        $scope.vm.activeTab=1;

        $scope.$on('$ionicView.beforeEnter', function() {
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        $scope.$watch("vm.activeTab", function(newValue, oldValue){
            if(newValue) {
                $timeout(function(){
                    $ionicScrollDelegate.resize();
                }, 500);
            }
        });

        function init(){
            if(!$scope.channels){
                $ionicLoading.show();
            }
            $scope.id = $stateParams['id'];
            //查询广告
            $scope.showAds(0);
            return showChannelImg();
        }

        function showChannelImg(){
            return InfoService.getChannelInfosByParentId($scope.id).then(function(data){
                $scope.channels = data;
                for(var i = 0; i < $scope.channels.length; i++) {
                    var channel = $scope.channels[i];
                    if(i == 0) {
                        $scope.vm.activeTab=channel.id;
                    }
                    (function(c){
                        var query;
                        if(c.remark == "otc") {
                            query = InfoService.getOtcProductsByChannelId(c.id);
                        } else {
                            query = InfoService.getProductsByChannelId(c.id);
                        }
                        query.then(
                            function(products) {
                                c.products =  products;
                                for(var i=0;i<c.products.length;i++){
                                    var fund = c.products[i];
                                    fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                                    if( c.remark == "otc") {
                                        if("0" == fund.otc_status){
                                            var days = CommonService.stringToDate(fund.ipo_begin_date).getTime() -new Date().getTime();
                                            fund.bengin_date_show = Math.round(days / (1000 * 60 * 60 * 24));
                                        }
                                        fund.sell_status = CommonService.getOtcSellStatus(fund.prod_status, fund.otc_status);
                                    }
                                }
                            }
                        )
                    })(channel);
                    $ionicLoading.hide();
                }
            });
        }

        $scope.showAds = function(index, type){

            var ad = "xcm_tfTeam_" + (index+1);
            WebService.wtAd(ad).then(function(data){
                if(data) {
                    $scope.ads = data;
                    var adKey = "ad_" + ad;
                    if((type || !LocalCacheService.get(adKey)) && $scope.ads.length > 0) {
                        var myPopup = $ionicPopup.show({
                            cssClass: "kDialog",
                            templateUrl: "product/ads.html",
                            scope: $scope,
                            buttons: [
                                { text: '确定' }
                            ]
                        });
                        LocalCacheService.set(adKey, true);
                    }
                }
            });
        };

    }

    ctrl.$inject = ['$scope','$filter','$ionicPopup','$q','$timeout','$ionicScrollDelegate','$stateParams',
        'CommonService','LocalCacheService','InfoService','WebService','$ionicLoading'];
    return ctrl;
});
